/*
 *  Copyright (C) 2010 Jesus Navalon i Pastor <jnavalon at redhermes dot net>
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * pn_Name.java
 *
 * Created on 28/12/2010, 10:33:19
 */
package kuasar.plugin.vmcreator.gui.tooltasks.AddHost;

import java.awt.Color;
import java.awt.event.KeyEvent;
import kuasar.plugin.Intercom.ODR;
import kuasar.plugin.utils.XML;
import kuasar.plugin.vmcreator.Config;
import org.jdom.Element;

/**
 *
 * @author Jesus Navalon i Pastor <jnavalon at redhermes dot net>
 */
public class pn_Name extends javax.swing.JPanel {

    pn_Summary parent = null;

    /** Creates new form pn_Name */
    public pn_Name(pn_Summary parent) {
        this.parent = parent;
        initComponents();
        if (parent.toolbar.name != null) {
            txt_Name.setText(parent.toolbar.name);
            txt_Name.setEditable(false);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txt_Name = new javax.swing.JTextField();
        lbl_Name = new javax.swing.JLabel();
        lbl_Info = new javax.swing.JLabel();

        setOpaque(false);

        txt_Name.setFont(new java.awt.Font("Dialog", 0, 18));
        txt_Name.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txt_Name.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txt_NameKeyReleased(evt);
            }
        });

        lbl_Name.setFont(new java.awt.Font("Droid Sans Mono", 1, 18));
        lbl_Name.setForeground(new java.awt.Color(204, 204, 204));
        lbl_Name.setText("System Name:");

        lbl_Info.setForeground(new java.awt.Color(255, 51, 51));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(lbl_Name))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lbl_Info, javax.swing.GroupLayout.DEFAULT_SIZE, 362, Short.MAX_VALUE)
                            .addComponent(txt_Name, javax.swing.GroupLayout.DEFAULT_SIZE, 362, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(89, 89, 89)
                .addComponent(lbl_Name)
                .addGap(18, 18, 18)
                .addComponent(txt_Name, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(59, 59, 59)
                .addComponent(lbl_Info)
                .addContainerGap(76, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void txt_NameKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_NameKeyReleased
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            save();
        }
        if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
            parent.loadSummary();
        }
        if (txt_Name.getBackground().equals(Color.pink)) {
            txt_Name.setBackground(Color.white);
        }
        if (lbl_Info.isVisible()) {
            lbl_Info.setVisible(false);
        }
    }//GEN-LAST:event_txt_NameKeyReleased

    protected void focusOnText() {
        txt_Name.requestFocus();
    }

    protected void save() {
        if (txt_Name.getText().trim().isEmpty()) {
            txt_Name.setBackground(Color.pink);
            return;
        }
        if(!XML.isCorrectName(txt_Name.getText())){
            lbl_Info.setVisible(true);
            lbl_Info.setText("Name can't start with a number and only can contains alfanumeric chars and spaces.");
            txt_Name.setBackground(Color.pink);
            return;
        } 
        
        Element root = XML.getElementOnPath(parent.toolbar.panel.onAir, (Element) ODR.getValue(Config.path + "." + Config.network));
        String name = XML.adaptName(txt_Name.getText());
        Element node = root.getChild(name);

        
        
        if (node != null) {
            if (parent.toolbar.name == null) {
                lbl_Info.setVisible(true);
                lbl_Info.setText("Name exists, Please check it and try again.");
                txt_Name.setBackground(Color.pink);
                return;
            }
        }else{
            root.removeChild(name);
        }
        
        parent.save(name, parent.toolbar.name != null);
        if(parent.toolbar.name==null)
            parent.exit();
        else
            parent.exitReEdit();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel lbl_Info;
    private javax.swing.JLabel lbl_Name;
    private javax.swing.JTextField txt_Name;
    // End of variables declaration//GEN-END:variables
}
